package com.example.demo.netty;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * netty api server管理类
 */
@Component
public class NettyServerManager implements ApplicationContextAware {

    private Map<String, NettyServer> servers = new HashMap<>();

    /**
     * 启动所有注册的netty api server
     */
    public void startAll(){
        for(Map.Entry<String, NettyServer> entry : servers.entrySet()){
            NettyServer server = entry.getValue();
            server.start();
        }
    }

    /**
     * 获取所有注册的Netty api server
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, NettyServer> beans = applicationContext.getBeansOfType(NettyServer.class);
        for(NettyServer server : beans.values()){
            this.servers.put(server.getName(), server);
        }
    }
}
